﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;

namespace Ue.CQRS.Repositories
{
    public class ConcurrentRepository<TKey, TValue> :ConcurrentDictionary<TKey,TValue>, IRepository<TKey, TValue>
    {
        public TValue Remove(TKey key)
        {
            TValue value = default(TValue);

            base.TryRemove(key, out value);

            return value;
        }

        public IEnumerable<TValue> FindBy(Expression<Func<TValue, bool>> predicate, int? skip, int? count)
        {
            var results = base.Values.Where(predicate.Compile());

            if (skip != null)
            {
                results=  results.Skip(skip.Value);
            }

            if (count != null)
            {
                results = results.Take(count.Value);
            }

            return results;
        }
    }
}
